package com.berwin.caper.view.components;

import com.berwin.caper.entity.Tag;
import com.berwin.caper.view.components.filetree.JFileTree;

import javax.swing.*;
import java.awt.*;

public class FileTreePanel extends JPanel {

    private IFileTreeAdapter adapter;

    private JPanel pnlTags;
    private JPanel pnlNavigator;
    private JTextField tfPath;

    private JFileTree tFiles;

    public FileTreePanel(IFileTreeAdapter adapter) {
        this.adapter = adapter;
        this.setLayout(new BorderLayout());

        this.initNorth();
        this.initCenter();
    }

    private void initNorth() {
        JPanel pnlNorth = new JPanel(new GridLayout(2, 1));
        pnlNorth.setBackground(Color.WHITE);
        this.add(pnlNorth, BorderLayout.NORTH);

        // 标签
        this.pnlTags = new JPanel(new FlowLayout(FlowLayout.CENTER));
        this.pnlTags.setToolTipText("书签栏");
        pnlTags.setBackground(Color.WHITE);
        JScrollPane scrollPane = new JScrollPane(this.pnlTags);
        scrollPane.setBorder(null);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        JScrollBar bar = scrollPane.getHorizontalScrollBar();
        System.out.println("height:" + bar.getSize().height);
        bar.setPreferredSize(new Dimension(bar.getPreferredSize().width, 1));
        pnlNorth.add(scrollPane);

        java.util.List<Tag> tags = this.adapter.getTags();
        int i = 0;
        for (Tag tag : tags) {
            ItemTagView itemTagView = this.adapter.getItemTagView(this.pnlTags, i, tag);
            this.pnlTags.add(itemTagView);
            i++;
        }
        // 路径
        this.pnlNavigator = new JPanel(new BorderLayout());
        this.pnlNavigator.setToolTipText("地址栏");
        this.pnlNavigator.setBackground(Color.WHITE);
        pnlNorth.add(this.pnlNavigator);
        this.tfPath = new JTextField();
        this.tfPath.setToolTipText("地址栏");
        this.tfPath.setBorder(null);
        Font font = new Font(this.tfPath.getFont().getName(), Font.PLAIN, 10);
        this.tfPath.setFont(font);
        this.tfPath.setText(this.adapter.getDefaultPath());
        this.pnlNavigator.add(tfPath, BorderLayout.CENTER);
    }

    private void initCenter() {
        // 目录树
        this.tFiles = new JFileTree(this.adapter.getDefaultPath());
        this.tFiles.setShowsRootHandles(true);
        this.tFiles.setEditable(true);
        JScrollPane scrollPane = new JScrollPane(this.tFiles);
        scrollPane.setBorder(null);
        this.add(scrollPane, BorderLayout.CENTER);
    }


}
